package com.itheima.core.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itheima.core.pojo.product.Brand;
import com.itheima.core.pojo.product.Color;
import com.itheima.core.pojo.product.Product;
import com.itheima.core.service.product.BrandService;
import com.itheima.core.service.product.ColorService;
import com.itheima.core.service.product.ProductService;

import cn.itcast.common.page.Pagination;

/**
 * @author zhang.cheng
 *
 *         2017年3月2日
 */
@Controller
public class ProductController {

	@Autowired
	private ProductService productService;

	@Autowired
	private BrandService brandService;

	@Autowired
	private ColorService colorService;

	@RequestMapping(value = "/product/list.do")
	public String toProductList(String name, Boolean isShow, Integer pageNo, Long brandId, Model model) {
		// 查詢品牌结果集 isDisplay==1 可用
		List<Brand> brands = brandService.selectBrandListByQuery(null, 1);
		model.addAttribute("brands", brands);
		// 分页查询商品
		Pagination paginationForProduct = productService.selectPaginationForProduct(name, isShow, pageNo, brandId);
		// 回显到页面
		model.addAttribute("paginationForProduct", paginationForProduct);
		model.addAttribute("name", name);
		model.addAttribute("isShow", isShow);
		model.addAttribute("pageNo", pageNo);
		model.addAttribute("brandId", brandId);
		return "product/list";
	}

	@RequestMapping(value = "/product/toAdd.do")
	public String toProductList(Model model) {
		// 查詢品牌结果集
		List<Brand> brands = brandService.selectBrandListByQuery(null, 1);
		model.addAttribute("brands", brands);
		// 查询颜色结果集
		List<Color> colors = colorService.selectColorList();
		model.addAttribute("colors", colors);
		return "product/add";
	}
	
	//添加商品
	@RequestMapping(value="/product/add.do")
	public String add(Product product,Model model){
		//保存
		productService.insertProduct(product);
		return "redirect:/product/list.do";
	}
	
	//商品上下架
	@RequestMapping(value="/product/isShow.do")
	public String isShow(Long[] ids) throws Exception{
		productService.isShow(ids);
		return "forward:/product/list.do";
	}
	
	

}
